package cn.hznc.controller.toolfork;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.configuration.MinioHelper;
import cn.hznc.domain.entity.ToolForkInfoEntity;
import cn.hznc.domain.request.toolfork.*;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.dto.ToolForkInfoImport;
import cn.hznc.dto.ToolForkInfoPriceImport;
import cn.hznc.excel.ToolForkInfoPriceReadListener;
import cn.hznc.excel.ToolForkReadListener;
import cn.hznc.exception.SystemException;
import cn.hznc.exception.ToolManageException;
import cn.hznc.controller.service.toolfork.ToolForkService;
import cn.hznc.permission.Permission;
import cn.hznc.utils.FileUploadUtil;
import cn.hznc.vo.ToolItemVo;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 刀具库接口层
 *
 * @author xiahuanyu
 * @date 2023/3/3
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(tags = "刀具库浏览接口")
@Slf4j
public class ToolForkController {
    @Resource
    public ToolForkService toolForkService;

    @Resource
    public MinioHelper minioHelper;

    /**
     * 刀具库查询
     *
     * @return
     */
    @ApiOperation(value = "刀具库查询")
    @RequestMapping(value = "/queryToolForkInfo", method = RequestMethod.POST)
    @Permission(title = "刀具库查询" , permissions = {"toolforkinfo.read"})
    public ResponseBody<PageResult<ToolForkInfoEntity>> queryToolForkInfo(@RequestBody QueryToolForkInfoReq queryToolForkInfoReq) {
        PageResult<ToolForkInfoEntity> result = toolForkService.queryToolForkInfo(queryToolForkInfoReq);
        return ResponseBody.success(result);
    }

    /**
     * 刀具库查询
     *
     * @param queryToolInfoReq queryToolInfoReq
     * @return
     */
    @ApiOperation(value = "刀具库查询")
    @RequestMapping(value = "/qryToolForkInfo", method = RequestMethod.POST)
    public ResponseBody<PageResult<ToolForkInfoEntity>> qryToolForkInfo(@RequestBody QueryToolInfoReq queryToolInfoReq) {
        PageResult<ToolForkInfoEntity> result = toolForkService.query(queryToolInfoReq);
        return ResponseBody.success(result);
    }

    /**
     * 刀具库查询
     *
     * @return
     */
    @ApiOperation(value = "刀具库查询")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public ResponseBody selectById(@RequestParam("id") Integer id) {
        return ResponseBody.success(toolForkService.selectById(id));
    }

    /**
     * 刀具库根据id查询
     *
     * @return
     */
    @ApiOperation(value = "刀具库根据id查询")
    @RequestMapping(value = "/qryToolForkInfoById", method = RequestMethod.GET)
    public ResponseBody qryToolForkInfoById(@RequestParam("id") Integer id) {
        return ResponseBody.success(toolForkService.qryToolForkInfoById(id));
    }

    /**
     * 刀具库删除
     *
     * @return
     */
    @ApiOperation(value = "刀具库删除")
    @RequestMapping(value = "/deleteToolForkInfo", method = RequestMethod.POST)
    @Log(title = "删除刀具零件类型" , businessType = BusinessType.DELETE)
    @Permission(title = "刀具库删除" , permissions = {"toolforkinfo.delete"})
    public ResponseBody<Boolean> deleteToolForkInfo(@RequestBody List<Integer> ids) {
        boolean deleteResult = toolForkService.delete(ids);
        return ResponseBody.success(deleteResult);
    }

    /**
     * 刀具库编辑
     *
     * @param entity entity
     * @return
     */
    @ApiOperation(value = "刀具库编辑")
    @RequestMapping(value = "/editToolForkInfo", method = RequestMethod.POST)
    @Log(title = "编辑刀具零件类型" , businessType = BusinessType.UPDATE)
    @Permission(title = "刀具库编辑" , permissions = {"toolforkinfo.update"})
    public ResponseBody<Boolean> editToolForkInfo(@RequestBody ToolForkInfoEntity entity) {
        boolean editResult = toolForkService.edit(entity);
        return ResponseBody.success(editResult);
    }

    /**
     * 刀具库新增
     *
     * @param entity entity
     * @return
     */
    @ApiOperation(value = "刀具库新增")
    @RequestMapping(value = "/addToolForkInfo", method = RequestMethod.POST)
    @Log(title = "新增刀具零件类型" , businessType = BusinessType.INSERT)
    @Permission(title = "刀具库新增" , permissions = {"toolforkinfo.add"})
    public ResponseBody<Boolean> addToolForkInfo(@RequestBody ToolForkInfoEntity entity) {
        boolean addResult = false;
        try {
            addResult = toolForkService.add(entity);
        } catch (ToolManageException toolManageException) {
            return ResponseBody.error(toolManageException.getCode(), toolManageException.getMsg());
        }
        return ResponseBody.success(addResult);
    }

    /**
     * 刀具库目录查询
     *
     * @param queryIndexReq queryIndexReq
     * @return
     */
    @ApiOperation(value = "刀具库目录查询")
    @RequestMapping(value = "/queryToolForkIndex", method = RequestMethod.POST)
    public ResponseBody<List<ToolItemVo>> queryToolForkIndex(@RequestBody QueryIndexReq queryIndexReq) {
        return ResponseBody.success(toolForkService.queryToolForkIndex(queryIndexReq));
    }

    /**
     * 刀具库目录参数查询
     *
     * @return
     */
    @ApiOperation(value = "刀具库目录参数查询")
    @RequestMapping(value = "/queryToolForkIndexParams", method = RequestMethod.GET)
    public ResponseBody queryToolForkIndexParams(@RequestParam Integer index) {
        return ResponseBody.success(toolForkService.queryToolForkIndexParams(index));
    }

    /**
     * 刀具库刀具详情参数编辑
     *
     * @return
     */
    @ApiOperation(value = "刀具库目录查询")
    @RequestMapping(value = "/editToolForkParams", method = RequestMethod.POST)
    @Log(title = "编辑刀具零件详情参数" , businessType = BusinessType.UPDATE)
    @Permission(title = "编辑刀具零件详情参数" , permissions = {})
    public ResponseBody<Boolean> editToolForkParams(@RequestBody ToolForkParamReq toolForkParamReq) {
        toolForkService.editToolForkParams(toolForkParamReq);
        return ResponseBody.success(Boolean.TRUE);
    }


    @RequestMapping(value = "/importToolForkInfo", method = RequestMethod.POST)
    public ResponseBody<Boolean> importToolForkInfo(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest httpServletRequest) {
        try {
            FileUploadUtil.checkCommonFile(multipartFile);
            ToolForkReadListener toolForkReadListener = new ToolForkReadListener();
            EasyExcel.read(multipartFile.getInputStream(), ToolForkInfoImport.class, toolForkReadListener).sheet().doRead();
            toolForkService.importToolForkInfo(toolForkReadListener.getImportData());
        } catch (IOException ex) {
            log.error("importToolForkInfo error========》", ex);
        } catch (SystemException systemException) {
            log.error("importToolForkInfo error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        } catch (ToolManageException toolManageException) {
            log.error("importToolForkInfo error========》msg：{}", toolManageException.getMsg());
            return ResponseBody.error(toolManageException.getCode(), toolManageException.getMsg());
        } finally {
            try {
                multipartFile.getInputStream().close();
            } catch (IOException e) {
                log.error("inputstream close error========》", e);
            }
        }
        return ResponseBody.success(Boolean.TRUE);
    }


    /**
     * 导出工件详细参数
     * @param response
     * @param toolForkInfoId
     */
    @RequestMapping(value = "/exportToolForkInfo", method = RequestMethod.GET)
    public void exportToolForkInfo(HttpServletResponse response, Integer toolForkInfoId) {
        try {
            toolForkService.exportToolForkInfo(response, toolForkInfoId);
        } catch (IOException e) {
            log.error("exportToolForkInfo error========》msg：{}" + e.getMessage());

        }
    }

    /**
     * 文件上传
     * @param image
     * @param twoDimensionalModel
     * @param threeDimensionalModel
     * @param idnr
     * @param partno
     * @return
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResponseBody uploadFile(@RequestPart(value = "image" , required = false) MultipartFile image,
                           @RequestPart(value = "twoDimensionalModel" , required = false) MultipartFile twoDimensionalModel,
                           @RequestPart(value = "threeDimensionalModel" , required = false) MultipartFile threeDimensionalModel,
                           @RequestPart(value = "threeDimensionalModelStl" , required = false) MultipartFile threeDimensionalModelStl,
                           @RequestPart("idnr") String idnr,
                           @RequestPart("partno") String partno) {
        try {
            return ResponseBody.success(toolForkService.uploadFile(image,twoDimensionalModel,threeDimensionalModel,threeDimensionalModelStl , idnr , partno));
        } catch (Exception e) {
            log.error("fileupload error========》msg：{}" + e.getMessage());
        }
        return ResponseBody.success();
    }

    /**
     * 文件上传
     * @return
     */
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    public ResponseBody uploadFiles(@RequestPart(value = "file") MultipartFile file,@RequestPart(value = "flag") String flag){
        try {
            return ResponseBody.success(toolForkService.uploadFiles(file , flag));
        } catch (Exception e) {
            log.error("fileupload error========》msg：{}" + e.getMessage());
        }
        return ResponseBody.success();
    }


    @RequestMapping(value = "/removeFile", method = RequestMethod.GET)
    public ResponseBody removeFile(@RequestParam String fileUrl) {
        try {
            toolForkService.removeFile(fileUrl);
        } catch (Exception e) {
            log.error("fileupload error========》msg：{}" + e.getMessage());
        }
        return ResponseBody.success();
    }

    @RequestMapping(value = "/getFile", method = RequestMethod.GET)
    public ResponseBody getFile(HttpServletResponse response) {
        try {
            return ResponseBody.success(minioHelper.downloadGetObjectUrl(response , "22223333344image"));
        } catch (Exception e) {
            log.error("fileupload error========》msg：{}" + e.getMessage());
        }
        return ResponseBody.error("11" , "error");
    }

    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public ResponseBody downloadFile(@RequestParam("fileUrl") String fileUrl){
//        try {
//            return ResponseBody.success(toolForkService.downloadFile(fileUrl));
//        } catch (Exception e) {
//            log.error("fileupload error========》msg：{}" + e.getMessage());
//        }
        return ResponseBody.success(toolForkService.downloadFile(fileUrl));
    }

    /**
     * 刀具浏览首页列表
     */
    @RequestMapping(value = "/getToolIndex", method = RequestMethod.GET)
    public ResponseBody getToolIndex(){
        return ResponseBody.success(toolForkService.getToolIndex());
    }

    @RequestMapping(value = "/importToolInfoData", method = RequestMethod.GET)
    public ResponseBody importToolInfoData(String params){
        return  ResponseBody.success(toolForkService.importToolInfoData(params));
    }

    @RequestMapping(value = "/updateToolPrice", method = RequestMethod.POST)
    @Log(title = "编辑刀具零件价格" , businessType = BusinessType.UPDATE)
    public ResponseBody updateToolPrice(@RequestPart("file") MultipartFile file){
        try {
            ToolForkInfoPriceReadListener toolForkInfoPriceReadListener = new ToolForkInfoPriceReadListener();
            FileUploadUtil.checkCommonFile(file);
            EasyExcel.read(file.getInputStream(), ToolForkInfoPriceImport.class, toolForkInfoPriceReadListener).autoCloseStream(true).sheet().doRead();
            return ResponseBody.success( toolForkService.updateToolPrice(toolForkInfoPriceReadListener.getImportData()));
        } catch (IOException ex) {
            log.error("importOrder error========》", ex);
        } catch (SystemException systemException) {
            log.error("importOrder error========》msg：{}", systemException.getMsg());
            return ResponseBody.error(systemException.getCode(), systemException.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    @RequestMapping(value = "/toolInfoManagement", method = RequestMethod.POST)
    @Permission(title = "物料基础信息管理查看" , permissions = {"partnoManagement.read"})
    public ResponseBody toolInfoManagement(@RequestBody ToolInfoManagementReq toolInfoManagementReq){
        return ResponseBody.success(toolForkService.toolInfoManagement(toolInfoManagementReq));
    }

    @RequestMapping(value = "/editToolInfoManagement", method = RequestMethod.POST)
    @Log(title = "编辑刀具零件寿命与价格" , businessType = BusinessType.UPDATE)
    @Permission(title = "编辑刀具零件寿命与价格" , permissions = {"partnoManagement.update"})
    public ResponseBody editToolInfoManagement(@RequestBody EditToolInfoManagementReq editToolInfoManagementReq){
        return ResponseBody.success(toolForkService.editToolInfoManagement(editToolInfoManagementReq));
    }

    @RequestMapping(value = "/editToolInfoManagementReload", method = RequestMethod.POST)
    @Log(title = "编辑刀具零件寿命与价格" , businessType = BusinessType.UPDATE)
    @Permission(title = "编辑刀具零件寿命与价格" , permissions = {"partnoManagement.update"})
    public ResponseBody editToolInfoManagementReload(@RequestBody EditToolInfoManagementReq editToolInfoManagementReq){
        return ResponseBody.success(toolForkService.editToolInfoManagementReload(editToolInfoManagementReq));
    }

    @RequestMapping(value = "/editToolLife", method = RequestMethod.GET)
    @Log(title = "编辑刀具零件寿命" , businessType = BusinessType.UPDATE)
    public ResponseBody editToolLife(){
        return ResponseBody.success(toolForkService.editToolLife());
    }

    @RequestMapping(value = "/editToolLifeReload", method = RequestMethod.GET)
    @Log(title = "编辑刀具零件寿命" , businessType = BusinessType.UPDATE)
    public ResponseBody editToolLifeReload(){
        return ResponseBody.success(toolForkService.editToolLifeReload());
    }

    @RequestMapping(value = "/changeToolForkIno", method = RequestMethod.GET)
    public ResponseBody changeToolForkIno(){
        return ResponseBody.success(toolForkService.changeToolForkIno());
    }


}
